const app = getApp()
Page({
  data: {
    name: '',
    mobile: '',
    remarks: '',
    buildingId: '',
    buildingName: '',
    schoolName: '',
    configState: false,
    configContent: '',
    dialogBodyStyle: {
      width: '70%',
      height: '250rpx'
    },
    shopObj: {}
  },
  onLoad: function (options) {
    if (JSON.stringify(options) !== '{}') {
      this.setData({
        buildingId: options.buildingId,
        buildingName: decodeURIComponent(options.buildingName),
        schoolName: decodeURIComponent(options.schoolName)
      })
    }
    this.getQueryAdminInfo()
  },
  onShow: function () {
    if (app.Store.get('openShop')) {
      this.setData({
        name: app.Store.get('openShop').name,
        mobile: app.Store.get('openShop').mobile,
        remarks: app.Store.get('openShop').remarks
      })
    }

  },
  getQueryAdminInfo() {
    app.Http.http({
      url: app.Api.QueryAdminInfo,
      method: 'POST',
      params: {}
    }).then(data => {
      this.setData({
        shopObj: data[0]
      })
    }).catch(err => {
      console.log(err)
    })
  },
  changeData(data) {//返回更新值
    if (JSON.stringify(data) !== "{}") {
      this.setData({
        buildingId: data.buildingId,
        buildingName: decodeURIComponent(data.buildingName),
        schoolName: decodeURIComponent(data.schoolName)
      })
    }
  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  close() {
    this.setData({
      configState: false
    })
  },
  formSubmit: function (e) {
    let subObj = e.detail.value
    if (!subObj.name) {
      this.setData({
        configState: true,
        configContent: '申请人姓名不能为空'
      })
      return
    }
    if (!subObj.mobile) {
      this.setData({
        configState: true,
        configContent: '申请人手机号不能为空'
      })
      return
    }else{
      if (!app.globalData.phoneCheck.test(subObj.mobile)){
        this.setData({
          configState: true,
          configContent: '请输入正确的手机号！'
        })
        return
      }
    }
    if (!subObj.buildingId) {
      this.setData({
        configState: true,
        configContent: '申请人学校楼栋不能为空'
      })
      return
    }

    app.Http.http({
      url: app.Api.OpenShop,
      method: 'POST',
      params: {
        name: subObj.name,
        mobile: subObj.mobile,
        remarks: subObj.remarks,
        buildingId: Number(subObj.buildingId)
      }
    }).then(data => {
      app.Store.remove('openShop')
      wx.showToast({
        title: '你的申请已提交成功！',
        icon: 'loading',
        duration: 1000,
        mask: !0,
      })
      setTimeout(() => {
        app.WxService.reLaunch('/pages/index/index')
      }, 1000)

    }).catch(err => {
      console.log(err)
    })
  },
  nameInput(e) {
    this.setData({
      name: e.detail.value
    })
  },
  mobileInput(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  remarksInput(e) {
    this.setData({
      remarks: e.detail.value
    })
  },
  selectAddress() {//开店
    let name = this.data.name
    let mobile = this.data.mobile
    let remarks = this.data.remarks
    app.Store.set('openShop', {
      name: name,
      mobile: mobile,
      remarks: remarks
    })
    app.WxService.navigateTo(`/pages/city-list/index`, { type: 'openShop' })
  }
})